import React from "react"

import http from "@/api/helper/request"

import {
	IMemberRateDetailItemResponse,
	IMemberRateDetailQueryParams,
	IMemberTeamRebateV2ItemResponse,
	IMemberTeamRebateV2QueryParams,
	IOutlineInfoRequestParams,
	IOutlineInfoResponse,
	IProxyAuditDetailHeaderQueryParams,
	IProxyAuditDetailHeaderResponse,
	IProxyDetailQueryParams,
	IProxyDetailResponse,
	IProxyRateDetailPageItemResponse,
	IProxyRateDetailPageQueryParams,
	IProxyTeamItemResponse,
	IProxyTeamQueryParams,
	IQueryListParams,
	IResponseItem
} from "./types"

const useApi = () => {
	const { useState } = React
	const [loading, setLoading] = useState(false)

	async function generalHttp<T>(url: string, body: object | undefined, config: object = {}) {
		setLoading(true)
		let ret = await http.post<T>(url, body, config)
		setLoading(false)
		return ret
	}

	async function generalGet<T>(url: string, body: object | undefined) {
		setLoading(true)
		const ret = await http.get<T>(url, body)
		setLoading(false)
		return ret
	}

	const list = (body?: IQueryListParams) =>
		generalHttp<Business.TableResourceType<IResponseItem>>("/proxyRebate/selectAuditRecord", body)

	const detailHeader = (body?: IProxyAuditDetailHeaderQueryParams) =>
		generalHttp<IProxyAuditDetailHeaderResponse>("/proxyRebate/queryProxyRebateAuditDetailHeader", body)

	const detail = (body?: IProxyDetailQueryParams) => generalHttp<IProxyDetailResponse>("/proxyDetail/queryDetail", body)

	const detailRatePage = (body?: IProxyRateDetailPageQueryParams) =>
		generalHttp<Business.TableResourceType<IProxyRateDetailPageItemResponse>>("/proxyRebate/getProxyRateDetailPage", body)

	const detailTeam = (body?: IProxyTeamQueryParams) =>
		generalHttp<IProxyTeamItemResponse>("/proxyRebate/getProxyTeamRebate", body)

	const getMemberRateDetailPage = (body?: IMemberRateDetailQueryParams) =>
		generalHttp<Business.TableResourceType<IMemberRateDetailItemResponse>>("/memberRebate/getMemberRateDetailPage", body)

	const getMemberRebateTeamRebateV2 = (body?: IMemberTeamRebateV2QueryParams) =>
		generalHttp<IMemberTeamRebateV2ItemResponse[]>("/memberRebate/getMemberTeamRebateV2", body)

	const getOutlineInfo = (body?: IOutlineInfoRequestParams) => generalGet<IOutlineInfoResponse>("/member/outlineInfo", body)

	return {
		loading,
		generalHttp,
		list,
		detailHeader,
		detail,
		detailRatePage,
		detailTeam,
		getOutlineInfo,
		getMemberRateDetailPage,
		getMemberRebateTeamRebateV2
	}
}

export default useApi
